/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_DAL_SERVICE_IMPL_HPP_
#define _DAL_FASTDDS_DAL_SERVICE_IMPL_HPP_

#include <mutex>
#include <atomic>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include "dalidl_typesupport_fastrtps_cpp/message_type_support.h"
#include "dal_fastdds/listener_impl/ServiceReqReaderListenerImpl.hpp"
#include "dal_fastdds/listener_impl/ServiceRespWriterListenerImpl.hpp"

using namespace eprosima::fastdds::dds;

class DalServiceImpl
{
public:
    explicit DalServiceImpl(std::string service_name,
                            const message_type_support_callbacks_t* req_ts,
                            const message_type_support_callbacks_t* resp_ts);
    ~DalServiceImpl();
    bool Clear();

    DomainParticipant* GetParticipant();
    void SetParticipant(DomainParticipant* participant);
    Publisher* GetCSPublisher();
    void SetCSPublisher(Publisher* publisher);
    Subscriber* GetCSSubscriber();
    void SetCSSubscriber(Subscriber* subscriber);
    Topic* GetReqTopic();
    Topic* GetRespTopic();
    DataWriter* GetRespWriter();
    void SetRespWriter(DataWriter* resp_writer);
    DataReader* GetReqReader();
    void SetReqReader(DataReader* req_reader);

    bool GetFastddsReqType(TopicDescription*& topic, TypeSupport& type);
    bool GetFastddsRespType(TopicDescription*& topic, TypeSupport& type);
    bool GetFastddsReqTopic(TopicDescription*& topic_desc);
    bool GetFastddsRespTopic(TopicDescription*& topic_desc);

public:
    ServiceReqReaderListenerImpl* req_reader_listener_;
    ServiceRespWriterListenerImpl* resp_writer_listener_;

    TypeSupport req_fastdds_type_;
    TypeSupport resp_fastdds_type_;

    const message_type_support_callbacks_t* req_ts_;
    const message_type_support_callbacks_t* resp_ts_;

private:
    DomainParticipant* f_participant_;
    Publisher* f_cs_publisher_;
    Subscriber* f_cs_subscriber_;
    DataReader* f_req_reader_;
    DataWriter* f_resp_writer_;

    std::string req_type_name_;
    std::string resp_type_name_;
    std::string req_topic_name_;
    std::string resp_topic_name_;
};

#endif  // _DAL_FASTDDS_DAL_SERVICE_IMPL_HPP_